from django.shortcuts import render, HttpResponse
from rest_framework.mixins import ListModelMixin, DestroyModelMixin, UpdateModelMixin, RetrieveModelMixin, \
    CreateModelMixin
# from rest_framework.viewsets import GenericViewSet
from rest_framework.viewsets import GenericViewSet
from .models import Leave
from .serializer import LeaveSerializer
from .page import MyPagination
# 导入第三方模块的过滤模块
from django_filters.rest_framework import DjangoFilterBackend
# 导入自动生成路由的装饰器模块
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.views import APIView
from utils.common_response import APIResponse
from libs.send_tx_number.send_msg import send_sms_by_mobile


# 新增请假单数据功能，
# http://127.0.0.1:8000/api/v1/leaveplatform/leave/
class LeaveView(GenericViewSet, ListModelMixin, DestroyModelMixin, UpdateModelMixin, RetrieveModelMixin,
                CreateModelMixin):
    queryset = Leave.objects.all().order_by("id")
    serializer_class = LeaveSerializer
    # 使用分页功能MyPagination
    # pagination_class = MyPagination
    # 基本分页导入
    pagination_class = MyPagination

    # 使用过滤功能
    filter_backends = [DjangoFilterBackend]
    # 按照名字和职务过滤
    filterset_fields = ['name', 'job']

    # http://127.0.0.1:8000/api/v1/leaveplatform/leave/post/
    # 新增请假单数据
    @action(methods=['GET'], detail=False)
    def post(self, request, *args, **kwargs):
        print(request.data)
        ser = self.get_serializer(data=request.data)  # # 相当于序列化类
        if ser.is_valid():
            # print(ser.validated_data)  # 打印校验之后的数据
            ser.save()
            # return Response({'status': 100, 'msg': '新增数据成功', "data": ser.validated_data})
            return Response({'status': 100, 'msg': '新增数据成功'})
        else:
            return Response({'status': 100, 'msg': '新增失败', "data": ser.errors})

    # 查询单条数据
    # http://127.0.0.1:8000/api/v1/leaveplatform/leave/2/
    # def retrieve(self, request, *args, **kwargs):
    #     res = super().retrieve(self, request, *args, **kwargs)
    #     # print(res.data)
    #     return APIResponse(data=res.data)


# # 假单管理删除，发送短信接口
# # http://127.0.0.1:8000/api/v1/leaveplatform/send_mobile/
# class send_mobile(APIView):
#     def post(self, request):
#         mobile = request.data.get('mobile')
#         code = request.data.get('reason')
#         # print(mobile, 'mobile_')
#         # print(code,'code_')
#         # 执行发送
#         send_sms_by_mobile(mobile, code)
#         return APIResponse(msg='发送成功')

# 假单管理删除，发送短信接口  测试
# http://127.0.0.1:8000/api/v1/leaveplatform/send_mobile/
class send_mobile(APIView):
    def post(self, request):
        mobile = request.data.get('mobile')
        code = request.data.get('reason')
        print(mobile, 'mobile_')
        print(code,'code_')
        # 执行发送
        # send_sms_by_mobile(mobile, code)
        return APIResponse(msg='发送成功')

